//
//  ScheduleDetailViewController.m
//  jiyujiaProgram
//
//  Created by Jame on 15/12/8.
//  Copyright © 2015年 中良 赵. All rights reserved.
//

#import "ScheduleDetailViewController.h"
#import "ScheduleDetailModel.h"

@interface ScheduleDetailViewController () <UIScrollViewDelegate,XLCycleScrollViewDatasource,XLCycleScrollViewDelegate,UIWebViewDelegate>

@end

@implementation ScheduleDetailViewController
{
    ScheduleDetailModel *_scheduleDetailModel;
    
    UIScrollView *_scrollView;
    UIView *_videoView;
    UIView *_photoView;
    XLCycleScrollView *_photo;
    UIImageView *_firImage;
    UIImageView *_secImage;
    UIImageView *_thirImage;
    UIImageView *_fourImage;
    UIImageView *_fifImage;
    UIImageView *_sixImage;
    
    UIView *_titleView;
    UILabel *_nameLabel;
    UILabel *_titleLabel;
    UILabel *_priceLabel;
    UILabel *_suitLabel;
    
    UIView *_descView;
    UILabel *_descLabel;
    
    UIView *_timeView;
    UILabel *_timeLabel;
    
    UIView *_typeView;
    UILabel *_typeLabel;
    NSMutableArray *_photoArray;
    UIWebView *_videoWeb;
    UIImageView *_fullImage;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self creatNavigationBarWithImage:nil title:@"课程详情"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];
    
    [self creatScrollView];
    [self creatVideoView];
    [self creatPhotoView];
    [self creatTitleView];
    [self creatDescView];
    [self creatTimeView];
    [self creatTypeView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self creatHttpRequest];
}

- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)creatHttpRequest
{
    NSDictionary *param = @{
                            @"course_Id":self.course_Id,
                            };
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(SCHEDULEDETAIL) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            _scheduleDetailModel = [[ScheduleDetailModel alloc] init];
            _scheduleDetailModel = [ScheduleDetailModel objectWithKeyValues:result];
            [self reloadViewWithModel:_scheduleDetailModel];
        }
    }];
}

- (void)reloadViewWithModel:(ScheduleDetailModel *)model
{
    
    if (model.course_video_IdsURL.length > 0) {
        [_videoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(150);
        }];
        
        NSString *videoUrl = [model.course_video_IdsURL substringToIndex:[model.course_video_IdsURL length] - 1];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:videoUrl]];
        [_videoWeb loadRequest:request];
    }else{
        [_videoWeb removeFromSuperview];
        [_videoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        
    }
    
    if (model.course_ImgIdsURL.length > 0) {
        
        if (model.course_video_IdsURL.length > 0) {
            [_photoView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(150);
                make.top.mas_equalTo(_videoView.mas_bottom).offset(5);
            }];
        }else{
            [_photoView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(150);
                make.top.mas_equalTo(_videoView.mas_bottom).offset(0);
            }];
        }
        
        
        if (_photoArray) {
            [_photoArray removeAllObjects];
        }else{
            _photoArray = [[NSMutableArray alloc] init];
        }
        
        NSString *imgUrl = [model.course_ImgIdsURL substringToIndex:[model.course_ImgIdsURL length] - 1];

        [_photoArray addObjectsFromArray:[imgUrl componentsSeparatedByString:@","]];
        [_photo reloadData];
    }else{
        [_photo removeFromSuperview];
        [_photoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
            make.top.mas_equalTo(_videoView.mas_bottom).offset(0);
        }];
    }
    
    _nameLabel.text = [NSString stringWithFormat:@"教师：%@",model.mName];
    _titleLabel.text = [NSString stringWithFormat:@"课程名称：%@",model.course_name];
    _priceLabel.text = [NSString stringWithFormat:@"费用：￥%@",model.price];
    _suitLabel.text = [NSString stringWithFormat:@"适用人群：%@",model.suits_the_crowd];
    
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
    [timeFormatter setDateFormat:@"HH:mm"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *date = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[model.class_time integerValue]/1000]];
    NSString *start = [timeFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[model.class_time integerValue]/1000]];
    NSString *end = [timeFormatter stringFromDate:[NSDate dateWithTimeInterval:60*60 sinceDate:[NSDate dateWithTimeIntervalSince1970:[model.class_time integerValue]/1000]]];
    NSString *dateString = [NSString stringWithFormat:@"%@   %@-%@",date,start,end];

    _timeLabel.text = [NSString stringWithFormat:@"上课时间：%@",dateString];
    _typeLabel.text = [NSString stringWithFormat:@"上课方式：%@",model.have_class_way];
    
    CGSize labelSize = STRING_SIZE_FONT_HEIGHT(ECSCREEN_W - 20, model.course_content, 13.f);
    NSLog(@"*****HEIGHT******%f",labelSize.height);
    
    [_descView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_greaterThanOrEqualTo(44+labelSize.height + 10);
    }];

    _descLabel.text = model.course_content;
    [_descLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_greaterThanOrEqualTo(labelSize.height);
    }];
    
    [self.view layoutIfNeeded];
    
    if (model.course_video_IdsURL.length > 0) {
        if (model.course_ImgIdsURL.length > 0) {
            _scrollView.contentSize = CGSizeMake(ECSCREEN_W, 562+labelSize.height);
        }else{
            _scrollView.contentSize = CGSizeMake(ECSCREEN_W, 412+labelSize.height);
        }
    }else{
        if (model.course_ImgIdsURL.length > 0) {
            _scrollView.contentSize = CGSizeMake(ECSCREEN_W, 407+labelSize.height);
        }else{
            _scrollView.contentSize = CGSizeMake(ECSCREEN_W, 252+labelSize.height);
        }
    }
}

- (void)creatScrollView
{
    if (self.isStudent) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, ECSCREEN_H - 64 - 40)];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, ECSCREEN_H - 40, ECSCREEN_W, 40);
        button.backgroundColor = NAVC_COLOR;
        [button setTitle:@"我要报名" forState:UIControlStateNormal];
        [button setTitleColor:ECCOLOR(255, 255, 255, 1) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }else{
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, ECSCREEN_H - 64)];
    }
    _scrollView.backgroundColor = BG_COLOR;
    _scrollView.contentSize = CGSizeMake(ECSCREEN_W, 242);
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
}


#warning ***********我要报名***********
- (void)buttonClick:(UIButton *)button
{
    NSLog(@"我要报名");
}


- (void)creatVideoView
{
    _videoView = [UIView new];
    _videoView.backgroundColor = [UIColor whiteColor];
    [_scrollView  addSubview:_videoView];
    
    [_videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_scrollView.mas_top).offset(0);
        make.centerX.mas_equalTo(_scrollView.mas_centerX);
        make.height.mas_greaterThanOrEqualTo(0);
        make.width.mas_equalTo(ECSCREEN_W);
    }];
    
    
    _videoWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 150)];
    _videoWeb.delegate = self;
    [_videoView addSubview:_videoWeb];
}



- (void)creatPhotoView
{
    
    _photoView = [UIView new];
    _photoView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_photoView];
    
    
    [_photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_greaterThanOrEqualTo(_videoView.mas_bottom).offset(0);
        make.centerX.mas_equalTo(_scrollView.mas_centerX);
        make.height.mas_greaterThanOrEqualTo(0);
        make.width.mas_equalTo(ECSCREEN_W);
    }];
    
    
    _photo = [[XLCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 150) withOutTimer:NO];
    _photo.delegate = self;
    _photo.datasource = self;
    _photo.pageControl.hidden = NO;
    [_photoView addSubview:_photo];
}


- (NSInteger)numberOfPages
{
    return _photoArray.count;
}



- (UIView *)pageAtIndex:(NSInteger)index
{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:_photo.frame];
    NSString *url = _photoArray[index];
    imageView.backgroundColor = [UIColor whiteColor];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:ECIMAGENAME(@"placeImage.png")];
    return imageView;
}


- (void)didClickPage:(XLCycleScrollView *)csView atIndex:(NSInteger)index
{
    
    NSString *url = _photoArray[index];
    _fullImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, ECSCREEN_H)];
    _fullImage.backgroundColor = ECCOLOR(0, 0, 0, .8);
    _fullImage.userInteractionEnabled = YES;
    _fullImage.contentMode = UIViewContentModeScaleAspectFit;
    [_fullImage sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:ECIMAGENAME(@"placeImage.png")];
    [self.view addSubview:_fullImage];
    
    UITapGestureRecognizer *singleTap =
    [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenImageView)];
    [_fullImage addGestureRecognizer:singleTap];

}


- (void)hiddenImageView
{
    [_fullImage removeFromSuperview];
}


- (void)creatTitleView
{
    _titleView = [UIView new];
    _titleView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_scrollView addSubview:_titleView];
    
    [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_photoView.mas_bottom).offset(5);
        make.centerX.mas_equalTo(_scrollView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(ECSCREEN_W, 90));
    }];
    
    
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, ECSCREEN_W - 20, 20)];
    _nameLabel.text = @"";
    _nameLabel.font = [UIFont systemFontOfSize:13.f];
    [_titleView addSubview:_nameLabel];
    
    
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, ECSCREEN_W - 20, 20)];
    _titleLabel.text = @"";
    _titleLabel.font = [UIFont systemFontOfSize:13.f];
    [_titleView addSubview:_titleLabel];
    
    _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 45, ECSCREEN_W - 20, 20)];
    _priceLabel.text = @"";
    _priceLabel.font = [UIFont systemFontOfSize:13.f];
    [_titleView addSubview:_priceLabel];
    
    _suitLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 65, ECSCREEN_W, 20)];
    _suitLabel.text = @"";
    _suitLabel.font = [UIFont systemFontOfSize:13.f];
    [_titleView addSubview:_suitLabel];
}

- (void)creatDescView
{
    _descView = [UIView new];
    _descView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_scrollView addSubview:_descView];
    
    [_descView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleView.mas_bottom).offset(5);
        make.width.mas_equalTo(ECSCREEN_W);
        make.height.mas_greaterThanOrEqualTo(44);
    }];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, ECSCREEN_W - 20, 44)];
    title.text = @"课程描述";
    title.font = [UIFont systemFontOfSize:13.f];
    [_descView addSubview:title];
    
    _descLabel = [UILabel new];
    [_descView addSubview:_descLabel];
    
    [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_descView.mas_top).offset(44);
        make.left.mas_equalTo(_descView.mas_left).offset(10);
        make.width.mas_equalTo(ECSCREEN_W - 20);
        make.height.mas_greaterThanOrEqualTo(0);
    }];
    
    _descLabel.font = [UIFont systemFontOfSize:13.f];
    _descLabel.numberOfLines = 0;
}

- (void)creatTimeView
{
    _timeView = [UIView new];
    _timeView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_scrollView addSubview:_timeView];
    
    [_timeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_descView.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(ECSCREEN_W, 44));
    }];
    
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, ECSCREEN_W - 20, 44)];
    _timeLabel.text = @"上课时间：";
    _timeLabel.font = [UIFont systemFontOfSize:13.f];
    [_timeView addSubview:_timeLabel];
}


- (void)creatTypeView
{
    _typeView = [UIView new];
    _typeView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [_scrollView addSubview:_typeView];
    
    [_typeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_timeView.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(ECSCREEN_W, 44));
    }];
    
    _typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, ECSCREEN_W - 20, 44)];
    _typeLabel.text = @"上课方式：";
    _typeLabel.font = [UIFont systemFontOfSize:13.f];
    [_typeView addSubview:_typeLabel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
